﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EmaMetadata;

namespace EmaEditor
{
    public partial class frmUserDefinedItem : Form
    {
        public LookupItem Item = null;

        public frmUserDefinedItem(string table)
        {
            InitializeComponent();

            txtTable.Text = table;
            linkLabel1.TabStop = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!Validate()) return;

            // Add this item to the 'user defined tables'
            Item = Factory.SetUserDefinedItem(txtTable.Text, txtNamespace.Text, int.Parse(txtID.Text), txtFactName.Text, txtNotes.Text);

            // Tell the caller we changed something
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://code.google.com/p/ema-metadata/wiki/UserDefinedItems");
        }

        private void btnSave_Validating(object sender, CancelEventArgs e)
        {
            txtTable.EnsureFieldNotEmpty(e, this.errorProvider1);
            txtNamespace.EnsureValidFQDN(e, this.errorProvider1);
            txtID.EnsureNumIsValid(e, errorProvider1);
            txtFactName.EnsureFieldNotEmpty(e, errorProvider1);
        }
    }
}
